/**
 * 
 */
package com.gr.staffpm.pages.projects;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.ChoiceRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.gr.staffpm.datatypes.Project;
import com.gr.staffpm.datatypes.Role;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.model.StringModel;
import com.gr.staffpm.pages.behaviour.FocusOnLoadBehavior;
import com.gr.staffpm.projects.model.ProjectPermission;
import com.gr.staffpm.projects.service.ProjectService;
import com.gr.staffpm.security.constants.CRUDProjectPermission;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.widget.jqgrid.EditableGrid;
import com.gr.staffpm.widget.jqgrid.column.AbstractEditablePropertyColumn;
import com.gr.staffpm.widget.jqgrid.column.EditableCellPanel;
import com.gr.staffpm.widget.jqgrid.column.EditableDropDownCellPanel;


/**
 * @author Graham Rhodes 3 May 2011 18:41:40
 */
public class SettingsTabPanel extends Panel {

    @SpringBean(name = "projectService")
    private ProjectService projectService;
    @SpringBean(name = "userService")
    private UserService userService;
    private static final long serialVersionUID = 1L;
    private final TextField<String> projectName = new TextField<String>("projectName", new StringModel(""));
    private final TextArea<String> description = new TextArea<String>("description", new StringModel(""));
    private final List<ProjectPermission> permissions = new ArrayList<ProjectPermission>();
    private final Project project;

    public SettingsTabPanel(String id, Project p) {
        super(id);
        this.project = p;

        Form<Project> form = new Form<Project>("editProjectForm") {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSubmit() {
                SettingsTabPanel.this.onSubmit();
            }
        };
        add(form);

        add(new FeedbackPanel("feedback"));

        projectName.setRequired(true);
        projectName.add(new FocusOnLoadBehavior());
        form.add(projectName);

        form.add(new EditableGrid<ProjectPermission>("grid", getColumns(), userService.getProjectPermissions(project), new ProjectPermission()) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onSave(ProjectPermission pp, AjaxRequestTarget target) {
                userService.createPermission(pp.getRole(), pp.getPermission(), project);
            }

            @Override
            protected void onDelete(ProjectPermission pp, AjaxRequestTarget target) {
                userService.deletePermission(pp.getPermission(), project);
            }

        }.setVersioned(false));

        form.add(description);

        Button cancel = new Button("cancelButton") {

            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit() {
                setResponsePage(Projects.class);
            }

        };
        cancel.setDefaultFormProcessing(false);
        form.add(cancel);
    }

    private List<AbstractEditablePropertyColumn<ProjectPermission>> getColumns() {
        List<AbstractEditablePropertyColumn<ProjectPermission>> columns = new ArrayList<AbstractEditablePropertyColumn<ProjectPermission>>();
        columns.add(new AbstractEditablePropertyColumn<ProjectPermission>(new Model<String>("Role"), "role") {

            private static final long serialVersionUID = 1L;

            @Override
            @SuppressWarnings({ "unchecked", "rawtypes" })
            public EditableCellPanel getEditableCellPanel(String componentId) {
                return new EditableDropDownCellPanel(componentId, this, userService.getAllRoles(), new ChoiceRenderer<Role>("role", "roleId"));
            }

        });
        columns.add(new AbstractEditablePropertyColumn<ProjectPermission>(new Model<String>("Permission"), "permission") {

            private static final long serialVersionUID = 1L;

            @Override
            @SuppressWarnings({ "unchecked", "rawtypes" })
            public EditableCellPanel getEditableCellPanel(String componentId) {
                return new EditableDropDownCellPanel(componentId, this, CRUDProjectPermission.valuesAsList(), new ChoiceRenderer<CRUDProjectPermission>("name", "ordinal"));
            }

        });
        return columns;
    }

    protected void onSubmit() {
        User currUser = userService.getCurrentUser();

        project.setProjectName(projectName.getValue());
        project.setDescription(description.getValue());
        project.setLastUpdated(new Date());
        project.setUpdatedBy(currUser);

        projectService.updateProject(project);
    }

}
